package com.minivision.sdktestdemo.utils;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.media.RingtoneManager;
import android.net.Uri;
import android.provider.MediaStore;

/**
 * Created by devtang on 2016/5/17.
 */
public class RingConfigUtils {
    public static boolean setMusicAsRingtones(Context context, String path) {
        ContentValues contentValues = new ContentValues();
        Uri newUri = null;
        Uri uri = MediaStore.Audio.Media.getContentUriForPath(path);
        ContentResolver contentResolver = context.getContentResolver();
        Cursor cursor = contentResolver.query(uri, null, MediaStore.Audio.Media.DATA + "=?", new String[]{path}, null);
        if (cursor.moveToNext()) {
            int _id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
            contentValues.put(MediaStore.Audio.Media.IS_RINGTONE, true);
            contentValues.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
            contentValues.put(MediaStore.Audio.Media.IS_ALARM, false);
            contentValues.put(MediaStore.Audio.Media.IS_MUSIC, false);
            contentResolver.update(uri, contentValues, MediaStore.Audio.Media.DATA + "=?", new String[]{_id + ""});
            newUri = ContentUris.withAppendedId(uri, _id);
            RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
            return true;
        } else {
            return false;
        }
    }
}
